package com.sky.service.impl;

import com.sky.constant.StatusConstant;

import com.sky.context.BaseContext;
import com.sky.entity.Orders;

import com.sky.mapper.*;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {
    @Autowired
    private WorkSpaceMapper workSpaceMapper;
    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private UserMapper userMapper;


//    /**
//     * 根据时间段统计营业数据
//     * @param begin
//     * @param end
//     * @return
//     */
//    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
//        /**
//         * 营业额：当日已完成订单的总金额
//         * 有效订单：当日已完成订单的数量
//         * 订单完成率：有效订单数 / 总订单数
//         * 平均客单价：营业额 / 有效订单数
//         * 新增用户：当日新增用户的数量
//         */
//
//        Map map = new HashMap();
//        map.put("begin",begin);
//        map.put("end",end);
//
//        //查询总订单数
//        Integer totalOrderCount = orderMapper.countByMap(map);
//
//        map.put("status", Orders.COMPLETED);
//        //营业额
//        Double turnover = orderMapper.sumByMap(map);
//        turnover = turnover == null? 0.0 : turnover;
//
//        //有效订单数
//        Integer validOrderCount = orderMapper.countByMap(map);
//
//        Double unitPrice = 0.0;
//
//        Double orderCompletionRate = 0.0;
//        if(totalOrderCount != 0 && validOrderCount != 0){
//            //订单完成率
//            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
//            //平均客单价
//            unitPrice = turnover / validOrderCount;
//        }
//
//        //新增用户数
//        Integer newUsers = userMapper.countByMap(map);
//
//        return BusinessDataVO.builder()
//                .turnover(turnover)
//                .validOrderCount(validOrderCount)
//                .orderCompletionRate(orderCompletionRate)
//                .unitPrice(unitPrice)
//                .newUsers(newUsers)
//                .build();
//    }
    /**
     * 统计营业数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);
        //1.查询总订单数
        Integer totalOrders = orderMapper.countByMap(map);
        map.put("status",Orders.COMPLETED);
        //2.有效订单：通过订单已完成状态查已完成订单数量
        Integer effectiveOrders = orderMapper.countByMap(map);
        //3.营业额：根据已完成状态查总金额
        Double turnover = orderMapper.sumByMap(map);

        Double orderCompletionRate = 0.0;
        Double unitPrice = 0.0;
        if (effectiveOrders != null && turnover != null) {
            //4.订单完成率：2/1
            orderCompletionRate = effectiveOrders.doubleValue() / totalOrders;
            //5.平均客单价：3/2
            unitPrice = turnover / effectiveOrders;
        }
        if (turnover == null){
            turnover = 0.0;
        }
        //6.新增用户
        Integer newUsers = userMapper.countByTime(begin,end);

        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .unitPrice(unitPrice)
                .turnover(turnover)
                .validOrderCount(effectiveOrders)
                .orderCompletionRate(orderCompletionRate)
                .build();

    }
    /**
     * 菜品总览
     * @return
     */
    @Override
    public DishOverViewVO viewDishes() {
        //获取在售菜品数量
        Integer sold = dishMapper.countByStatus(StatusConstant.ENABLE);
        //获取停售菜品数量
        Integer discontinued = dishMapper.countByStatus(StatusConstant.DISABLE);
        //塞进去
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO viewSetmeals() {
        //获取在售套餐数量
        Integer sold = setmealMapper.countByStatus(StatusConstant.ENABLE);
        //获取停售套餐数量
        Integer discontinued = setmealMapper.countByStatus(StatusConstant.DISABLE);
        //塞进去
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }


    /**
     * 统计营业数据
     * @return
     */
    @Override
    public BusinessDataVO viewData() {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */
        //1.查询总订单数
        Integer totalOrders = orderMapper.countTotalOrders();
        //2.有效订单：通过订单已完成状态查已完成订单数量
        Integer effectiveOrders = orderMapper.countByStatus(Orders.COMPLETED);
        //3.营业额：根据已完成状态查总金额
        Double turnover = orderMapper.sumByStatus(Orders.COMPLETED);
        Double orderCompletionRate = 0.0;
        Double unitPrice = 0.0;
        if (effectiveOrders != null && turnover != null) {
            //4.订单完成率：2/1
            orderCompletionRate = effectiveOrders.doubleValue() / totalOrders;
            //5.平均客单价：3/2
            unitPrice = turnover / effectiveOrders;
        }
        //6.新增用户
        Integer newUsers = orderMapper.countByStatus(Orders.COMPLETED);

        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .unitPrice(unitPrice)
                .turnover(turnover)
                .validOrderCount(effectiveOrders)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }
    @Override
    public OrderOverViewVO ordersList() {
            OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
            //获取全部订单
            Integer allOrders = orderMapper.getAllOrders();
            //获取已经取消数量
            Integer cancelledOrders = orderMapper.countStatus(6);
            //获取已经完成数量
            Integer completedOrders = orderMapper.countStatus(5);
            //待派送数量
            Integer deliveredOrders = orderMapper.countStatus(3);
            //待接单数量
            Integer waitingOrders = orderMapper.countStatus(2);
            //塞进去
            orderOverViewVO.setAllOrders(allOrders);
            orderOverViewVO.setCancelledOrders(cancelledOrders);
            orderOverViewVO.setCompletedOrders(completedOrders);
            orderOverViewVO.setDeliveredOrders(deliveredOrders);
            orderOverViewVO.setWaitingOrders(waitingOrders);
            return orderOverViewVO;

    }
}
